package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.pojo.FiPtp;
import com.miplus.ccrm.core.pojo.FiPtpHist;
import com.miplus.ccrm.core.service.crd.FiPtpHistService;
import com.miplus.ccrm.core.service.crd.FiPtpService;
import com.miplus.ccrm.core.mapper.FiPtpMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author wangzl
* @description 针对表【fi_ptp】的数据库操作Service实现
* @createDate 2024-11-07 17:10:25
*/
@Service
public class FiPtpServiceImpl extends ServiceImpl<FiPtpMapper, FiPtp>
    implements FiPtpService{
    @Resource
    FiPtpHistService  fiPtpHistService;

    @Override
    public CmdResVo addPtp( FiPtp fiPtp, String usrId)
    {
        //获取当前的Ptp
        System.out.println("----------------sPtp1 ........" + fiPtp.getPtpType());
        if(ObjectUtils.isNull(fiPtp.getCreditcaseid()))
            return new CmdResVo(0,"没有指定任务");
        if(ObjectUtils.isNull(fiPtp.getPtpType()))
            return new CmdResVo(0,"没有承诺还款定义");
        if(( fiPtp.getPtpType().equals("A") || fiPtp.getPtpType().equals("B") ) && ObjectUtils.isNull(fiPtp.getPtpDate()) )
            return new CmdResVo(0,"没有承诺还款时间");
        if( ObjectUtils.isNotNull(fiPtp.getPtpDate()) && fiPtp.getPtpDate().before(new Date()))
        {
            return new CmdResVo(0,"时间不能比当前时间晚");
        }
        //try {
            FiPtp fiPtp1 = baseMapper.selectOne(new LambdaQueryWrapper<FiPtp>()
                    .eq(FiPtp::getCreditcaseid, fiPtp.getCreditcaseid()));
            if (fiPtp1 != null && ObjectUtils.isNotNull(fiPtp1.getPtpDate()) && fiPtp1.getPtpDate().before(new Date())) {
                FiPtpHist fiPtpHist = new FiPtpHist();
                fiPtp1.setStatus(2); // 无效。
                BeanUtils.copyProperties(fiPtp1, fiPtpHist);
                fiPtpHistService.save(fiPtpHist);
            }
            else if( fiPtp1 != null ){
                fiPtp1.setMddt( new Date());
                fiPtp1.setMdusr( usrId);
                fiPtp1.setPtpType( fiPtp.getPtpType());
                //A 承诺还款，B：部分还款，C：拒绝还款，D：已还款
                fiPtp1.setPtpAmt(fiPtp.getPtpAmt());
                fiPtp1.setPtpDate(fiPtp.getPtpDate());
                if( fiPtp.getPtpType().equals("A") || fiPtp.getPtpType().equals("B"))

                    fiPtp1.setStatus(1);
                else if( fiPtp.getPtpType().equals("C") )
                    fiPtp1.setStatus(3);
                else
                    fiPtp1.setStatus(4);
                updateById(fiPtp1);
                if(fiPtp.getPtpType().equals("C") ||fiPtp.getPtpType().equals("D")  )
                {
                    //记录历史。
                    FiPtpHist fiPtpHist = new FiPtpHist();
                    BeanUtils.copyProperties(fiPtp1, fiPtpHist);
                    fiPtpHistService.save(fiPtpHist);
                }
            }
            else {
                //创建新的承诺还款。
                fiPtp.setCrdt(new Date());
                fiPtp.setCrusr(usrId);
                if( fiPtp.getPtpType().equals("A") || fiPtp.getPtpType().equals("B"))
                    fiPtp.setStatus(1);
                else if( fiPtp.getPtpType().equals("C") )
                    fiPtp.setStatus(3);
                else
                    fiPtp.setStatus(4);
                save(fiPtp);
            }
        //}catch(Exception e){
        //    return new CmdResVo(0, e.toString());
        //}
        return new CmdResVo(1, "OK");
    }
}




